package com.base.controller;

import com.base.po.tbk.TbkBrandCategory;
import com.base.po.tbk.TbkBrandShop;
import com.base.service.TbkBrandCategoryService;
import com.base.service.TbkBrandShopService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 大牌店铺专场
 *
 * @author K
 */
@Controller
@RequestMapping("/admin")
public class index_TbkBrandShop {

    @Autowired
    private TbkBrandShopService tbkBrandShopService; // 大牌店铺信息
    @Autowired
    private TbkBrandCategoryService tbkBrandCategoryService;

    // 分类广告首页
    @RequestMapping("/tbkBrandShopIndex")
    public ModelAndView tbkBrandShopIndex(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();

        String adminUserId = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
        if (adminUserId == null) return new ModelAndView("jsp/loginCheck"); // 未登录

        Page page = new Page();
        List<TbkBrandShop> tbkBrandShopList = null;

        page.setCurrentPageNo(1);
        page.setRecordTotal(tbkBrandShopService.countAll(adminUserId, null, null));
        tbkBrandShopList = tbkBrandShopService.selectAll(page, adminUserId, null, null, 1);

        List<TbkBrandCategory> tbkBrandCategory = tbkBrandCategoryService.selectByLevel1(adminUserId, adminUserId);

        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("tbkBrandShop", tbkBrandShopList);
        request.setAttribute("tbkBrandCategory", tbkBrandCategory);
        request.setAttribute("classify", "分类");
        modelAndView.setViewName("jsp/index_TbkBrandShop");
        return modelAndView;
    }

    @RequestMapping("/selectTbkBrandShop")
    public ModelAndView selectTbkBrandShop(HttpServletRequest request, Integer curPage, String categoryId, String name) throws Exception {
        // 设置session缓存
        if (curPage == null) {
            if (request.getSession().getAttribute("curPage") != null) {
                curPage = (Integer) request.getSession().getAttribute("curPage");
            } else {
                curPage = 1;
            }
        }

        String fId = null;
        String fName = null;
        if (categoryId != null && !categoryId.equals("") && categoryId.equals("分类")){
            fId = null;
        }else {
            fId = categoryId;
        }
        if (name != null && !name.equals("")){
            fName = name;
        }

        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");

        Page page = new Page();
        List<TbkBrandShop> tbkBrandShopList = null;

        page.setCurrentPageNo(curPage);
        page.setRecordTotal(tbkBrandShopService.countAll(adminUserId, fId, fName));
        tbkBrandShopList = tbkBrandShopService.selectAll(page, adminUserId, fId, fName, 1);

        List<TbkBrandCategory> tbkBrandCategory = tbkBrandCategoryService.selectByLevel1(adminUserId, adminUserId);

        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("tbkBrandShop", tbkBrandShopList);
        request.setAttribute("tbkBrandCategory", tbkBrandCategory);
        request.setAttribute("classify", categoryId);
        request.setAttribute("name", name);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_TbkBrandShop");
        return modelAndView;
    }

    // 跳转添加分类
    @RequestMapping("/addTbkBrandShop")
    public String addTbkBrandShop(HttpServletRequest request) {
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        List<TbkBrandCategory> tbkBrandCategory = tbkBrandCategoryService.selectByLevel1(adminUserId, adminUserId);
        request.setAttribute("tbkBrandCategory", tbkBrandCategory);
        return "jsp/add_TbkBrandShop";
    }

    // 跳转修改分类
    @RequestMapping("/updateTbkBrandShop")
    public String updateTbkBrandShop(HttpServletRequest request, String id) {
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        List<TbkBrandCategory> tbkBrandCategory = tbkBrandCategoryService.selectByLevel1(adminUserId, adminUserId);
        TbkBrandShop tbkBrandShop = tbkBrandShopService.selectByPrimaryKey(id);
        request.setAttribute("tbkBrandCategory", tbkBrandCategory);
        request.setAttribute("sShop", tbkBrandShop);
        request.setAttribute("id", id);
        return "jsp/update_TbkBrandShop";
    }
}
